package com.csxy.m2020_note;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.csxy.m2020_note.sql.NoteDbHelper;

public class EditActivity extends AppCompatActivity {

    private EditText titleEdit;
    private EditText bodyEdit;
    private Button confirmBtn;
    int id=-999;
    NoteDbHelper noteDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        titleEdit = (EditText) findViewById(R.id.title);
        bodyEdit = (EditText) findViewById(R.id.body);
        confirmBtn = (Button) findViewById(R.id.confirm);

        noteDbHelper=new NoteDbHelper(EditActivity.this);
        noteDbHelper.open();
        Intent intent=getIntent();
        if(intent!=null){
            String titleStr=intent.getStringExtra("title");
            String bodyStr=intent.getStringExtra("body");
            id=intent.getIntExtra("id",-999);
            titleEdit.setText(titleStr);
            bodyEdit.setText(bodyStr);
        }


        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO  待完成的工作：：将数据保存到数据库
                //更新数据
                noteDbHelper.updateNote(id,  titleEdit.getText().toString(),bodyEdit.getText().toString());
              /*  Toast.makeText(EditActivity.this,
                        titleEdit.getText().toString()+bodyEdit.getText().toString(),
                        Toast.LENGTH_SHORT).show();*/

                finish();
            }
        });
    }
}
